﻿package com.dykj.apps.web.sysmanager.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.dykj.apps.web.sysmanager.dao.IFunctionDAO;
import com.dykj.apps.web.sysmanager.dao.ISysAclDAO;
import com.dykj.apps.web.sysmanager.dao.po.SysAcl;
import com.dykj.apps.web.sysmanager.dao.po.SysRole;
import com.dykj.apps.web.sysmanager.dto.SysRoleDTO;
import com.dykj.platform.framework.exception.ApplicationException;
import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.utils.BeanUtils;
import com.dykj.platform.iadapter.utils.TreeHelper;

public class SysAclBSImpl implements ISysAclBS {
	private ISysAclDAO dao;
	private IFunctionDAO fundao;
	/**
	 * 查询userid创建的所有角色
	 */
	public List findAllRole(String userid)throws ApplicationException {
		List<SysRoleDTO> dtoList = new ArrayList<SysRoleDTO>();
		try {
			List roleList = dao.findAllRole(userid);
			for(int i=0;i<roleList.size();i++){
				SysRoleDTO sysRoleDTO = new SysRoleDTO();
				BeanUtils.copyProperties(sysRoleDTO, roleList.get(i));
				dtoList.add(sysRoleDTO);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return dtoList;
	}
	/**
	 * 查询功能点 roleid被分配的角色,croleid当前用户的角色
	 */
	public String findFunctionJSON(String roleid,String croleid) throws ApplicationException {
		String json = "";
		try {
			List list = fundao.findAllFunctionId(roleid, croleid);
			json = TreeHelper.getAllChildrenJSONTrees(list);
		} catch (DatabaseException e) {
			e.printStackTrace();
			throw new ApplicationException(e);
		}
		return json;
	}
	/**
	 * 保存角色授权
	 */
	public int saveFunctionAcl(String roleid, String[] functionid) throws ApplicationException {
		try {
			List oldacl = dao.findFunctionidByRoleid(roleid);//查询以前所有的功能授权
			Iterator oldAclIt = oldacl.iterator();
			dao.deleteAcl(roleid);//删除以前的功能授权
			if(null!=functionid){
			for(int i=0;i<functionid.length;i++){ //保存新的功能授权
				
				SysAcl acl = new SysAcl();
				acl.setAclid(Long.valueOf((dao.getKeyIndex("aclid", "sysacl"))));
				acl.setChecked("1");
				acl.setFunctionid(functionid[i]);
				acl.setRoleid(Long.valueOf(roleid));
				dao.save(acl);
				while(oldAclIt.hasNext()){ //找出删除的功能点
					SysAcl old = (SysAcl)oldAclIt.next();
					if(old.getFunctionid().equals(acl.getFunctionid())){
						oldAclIt.remove();
					}
				}
			}
			}
			//本级功能点减少，下级自动减少
			if(oldAclIt.hasNext()){
				List roles = dao.findRolesByRoleid(roleid);
				for(int i=0;i<roles.size();i++){
					Map mapid = (Map)roles.get(i);
					String subid = mapid.get("roleid").toString();
					List subacl = dao.findFunctionidByRoleid(subid);// 查询该角色的原有权限
					for(int j = 0;j<subacl.size();j++){	//原有权限和上面授权时删除的功能点做比较,如果原有权限中存在则删除
						SysAcl sysacl = (SysAcl) subacl.get(j);
						while(oldAclIt.hasNext()){
							SysAcl oacl = (SysAcl) oldAclIt.next();
							if(oacl.getFunctionid().equals(sysacl.getFunctionid())){
								dao.delete(sysacl);
							}
						}
					}
				
				}
			}
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	public IFunctionDAO getFundao() {
		return fundao;
	}

	public void setFundao(IFunctionDAO fundao) {
		this.fundao = fundao;
	}
	public ISysAclDAO getDao() {
		return dao;
	}

	public void setDao(ISysAclDAO dao) {
		this.dao = dao;
	}
}
